using System.Reflection;
using UniversalAdmin.Api.Filters;
using UniversalAdmin.Application.Features.Menus.Queries.GetMenuTree;
using UniversalAdmin.Application.Services;
using UniversalAdmin.Domain.Interfaces;
using UniversalAdmin.Infrastructure;
using UniversalAdmin.Infrastructure.Services;

namespace UniversalAdmin.Api.Extensions;

public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 添加MediatR服务
    /// </summary>
    public static IServiceCollection AddMediatRServices(this IServiceCollection services)
    {
        services.AddMediatR(cfg =>
            cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

        services.AddMediatR(cfg =>
            cfg.RegisterServicesFromAssembly(typeof(GetMenuTreeQuery).Assembly));

        return services;
    }

    /// <summary>
    /// 添加基础设施服务
    /// </summary>
    public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
    {
        // 注册仓储
        services.AddScoped<IUnitOfWork, UnitOfWork>();

        // 注册服务
        services.AddScoped<IJwtService, JwtService>();
        services.AddScoped<IAuthService, AuthService>();
        services.AddScoped<MenuTreeBuilder>();

        // 注册密码哈希服务
        services.AddScoped<IPasswordHasher, PasswordHasher>();


        services.AddScoped<AuditLogActionFilter>();
        services.AddScoped<ExceptionLogFilter>();

        return services;
    }

    /// <summary>
    /// 添加JWT认证
    /// </summary>
    // public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, IConfiguration configuration)
    // {
    //   services.AddAuthentication(options =>
    //   {
    //     options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    //     options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    //   })
    //   .AddJwtBearer(options =>
    //   {
    //     options.TokenValidationParameters = new TokenValidationParameters
    //     {
    //       ValidateIssuer = true,
    //       ValidateAudience = true,
    //       ValidateLifetime = true,
    //       ValidateIssuerSigningKey = true,
    //       ValidIssuer = configuration["Jwt:Issuer"],
    //       ValidAudience = configuration["Jwt:Audience"],
    //       IssuerSigningKey = new SymmetricSecurityKey(
    //                 Encoding.UTF8.GetBytes(configuration["Jwt:SecretKey"]!))
    //     };
    //   });

    //   return services;
    // }

    /// <summary>
    /// 添加权限策略
    /// </summary>
    public static IServiceCollection AddAuthorizationPolicies(this IServiceCollection services)
    {
        services.AddAuthorization(options =>
        {
            // 用户权限
            options.AddPolicy("user.view", policy => policy.RequireRole("Admin",
            "SuperAdmin"));
            options.AddPolicy("user.create", policy => policy.RequireRole("Admin",
             "SuperAdmin"));
            options.AddPolicy("user.update", policy => policy.RequireRole("Admin",
             "SuperAdmin"));
            options.AddPolicy("user.delete", policy => policy.RequireRole("SuperAdmin", "Admin"));

            // 菜单权限
        //     options.AddPolicy("menu.view", policy =>
        //           policy.RequireClaim("Permission", "menu.view"));
        //     options.AddPolicy("menu.create", policy =>
        // policy.RequireClaim("Permission", "menu.create"));
        //     options.AddPolicy("menu.update", policy =>
        // policy.RequireClaim("Permission", "menu.update"));
        //     options.AddPolicy("menu.delete", policy =>
        // policy.RequireClaim("Permission", "menu.delete"));

        //     // 角色权限
        //     options.AddPolicy("role.view", policy =>
        // policy.RequireClaim("Permission", "role.view"));
        //     options.AddPolicy("role.create", policy =>
        // policy.RequireClaim("Permission", "role.create"));
        //     options.AddPolicy("role.update", policy =>
        // policy.RequireClaim("Permission", "role.update"));
        //     options.AddPolicy("role.delete", policy =>
        // policy.RequireClaim("Permission", "role.delete"));
        options.AddPolicy("menu.view", policy =>  policy.RequireAuthenticatedUser());
        options.AddPolicy("menu.create", policy => policy.RequireRole("Admin", "SuperAdmin"));
        options.AddPolicy("menu.update", policy => policy.RequireRole("Admin", "SuperAdmin"));
        options.AddPolicy("menu.delete", policy => policy.RequireRole("SuperAdmin", "Admin"));

        // 角色权限
        options.AddPolicy("role.view", policy => policy.RequireAuthenticatedUser());
        options.AddPolicy("role.create", policy => policy.RequireRole("Admin", "SuperAdmin"));
        options.AddPolicy("role.update", policy => policy.RequireRole("Admin", "SuperAdmin"));
        options.AddPolicy("role.delete", policy => policy.RequireRole("SuperAdmin", "Admin"));
            //管理员权限
            options.AddPolicy("admin.system.manage", p =>
        p.RequireRole("SuperAdmin", "Admin"));
        });

        return services;
    }
}